/* 
 * File:   CSocket-Inl.h
 * Copyright (c) 2011. Silviu Caragea <silviu@intelliproject.net>
 * Created on January 31, 2011, 11:16 PM
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions
 * are met:
 *  1.Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  2.Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *  3.All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *       This product includes software developed by the University of
 *       California, Berkeley and its contributors.
 *  4.Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef CSOCKET_INL_H
#define	CSOCKET_INL_H

#include "CSocket.h"

namespace litevent{
namespace util{

inline CSocket::CSocket(int dwSocket,sockaddr_in addr):m_sock(dwSocket), m_sockaddr(addr)
{

}

inline CSocket::~CSocket()
{
    this->Close();
}

inline void CSocket::Close(void)
{
#ifndef WIN32
    close(m_sock);
#else
    closesocket(m_sock);
#endif

}

inline int CSocket::Read(void * pData, unsigned int iLen)
{
    if (!pData || !iLen)
        return ( -1);

    return recv(m_sock, (char *) pData, iLen, 0);
}

inline int CSocket::Write(const void * pData, unsigned int iLen)
{
    if (!pData || !iLen)
        return (-1);

    return send(m_sock, pData, iLen, 0);
}

inline int CSocket::countToRead() const
{
     int toread = 0;

     if (ioctl(m_sock, FIONREAD, &toread)!= 0)
         throw CException("ioctl failed!");

     return toread;
}

inline int CSocket::getSocket() const
{
    return this->m_sock;
}

inline bool CSocket::isValidSocket() const
{
    return (this->m_sock != -1);
}

inline void CSocket::Reset(unsigned int dwPort)
{
    m_sock = socket(AF_INET, SOCK_STREAM, 0);

    if (m_sock == -1)
        throw CException("socket() failed");

    memset(&m_sockaddr, 0, sizeof (sockaddr));
    m_sockaddr.sin_family = AF_INET;
    m_sockaddr.sin_port = htons(dwPort);
    //m_sockaddr.sin_addr.s_addr = INADDR_ANY;
    inet_aton("127.0.0.1",&m_sockaddr.sin_addr);
}

inline void CSocket::setReuseAddress(bool value)
{
    if(setsockopt(m_sock, SOL_SOCKET, SO_REUSEADDR, (void*)&value,sizeof(value))== -1)
        throw CException("setsockopt failed");
}

inline bool CSocket::getReuseAddress()
{
    int optVal;
    socklen_t optLen = sizeof(socklen_t);

    if(getsockopt(m_sock, SOL_SOCKET, SO_REUSEADDR, &optVal, &optLen)==-1)
        throw CException("getsockopt failed");

    return optVal;
}

}}
#endif	/* CSOCKET_INL_H */

